import { GetGood, postgood } from "../../api/good3c";
import { GetCart, PostCart, DelCart, PatchCart } from "../../api/cart";
import { toast, to } from "../../utils/wxapi";
import { err } from "../../utils/console";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cartshow: false,
    list: [] as any,
    page: 1,
    limit: 20,
    cha: "",
    show: false,
    item: { id: "" },
    quantity: 1,
    cart_list: [],
    isLoading: false,
    price: 0,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getlist();
  },
  img(e: any) {
    console.log(e);
    const { src } = e.currentTarget.dataset;
    console.log(src);
    wx.previewImage({
      current: src,
      urls: [src],
    });
  },
  async getlist() {
    try {
      const { page, limit } = this.data;
      const res = await GetGood({
        page,
        limit,
      });
      console.log(res);
      if (res.statusCode === 200) {
        const newList = res.data;
        const updatedList = [...this.data.list, ...newList];
        this.setData({
          list: updatedList,
          page: this.data.page + 1,
          isLoading: false,
        });
      }
    } catch (error) {
      err(error);
    }
  },
  async getcart() {
    try {
      const data = { all: 1 };
      const res: any = await GetCart(data);
      console.log(res);
      if (res.statusCode === 200) {
        let price = this.cartprice(res.data);
        this.setData({ cart_list: res.data, price });
      }
    } catch (err) {
      console.error(err);
    }
  },
  cartprice(arr: any[]) {
    let price: number = 0;
    arr.forEach((e: any) => {
      price += e.quantity * e.goods.price;
    });
    return price;
  },
  async search() {
    try {
      const data = { key_word: this.data.cha };
      const res: any = await postgood(data);
      console.log(res);
      if (res.statusCode === 200) {
        this.setData({ list: res.data });
      }
    } catch (err) {
      console.error(err);
    }
  },
  add(e: any) {
    const { item } = e.currentTarget.dataset;
    this.setData({ item, show: true });
  },
  addcrea() {
    this.getcart();
    this.setData({ cartshow: true });
  },
  onclose() {
    this.setData({ show: false, cartshow: false });
  },
  onChange(e: any) {
    console.log(e);
    this.setData({ quantity: e.detail });
  },
  async care() {
    try {
      const { item, quantity } = this.data;
      const data = {
        goods_id: item.id,
        quantity,
      };
      const res: any = await PostCart(data);
      console.log(res);
      if (res.statusCode === 200) {
        toast("添加成功");
        this.getcart();
        this.setData({ show: false });
      }
    } catch (err) {
      err(err);
    }
  },
  async change(e: any) {
    try {
      console.log(e);
      const quantity = e.detail;
      const id = e.currentTarget.dataset.id;
      if (quantity === 0) {
        const delRes: any = await DelCart(id);
        console.log(delRes);
        if (delRes.statusCode === 200) {
          this.getcart();
        }
      } else {
        const data = { quantity };
        const patchRes: any = await PatchCart({ id, data });
        console.log(patchRes);
        if (patchRes.statusCode === 200) {
          this.getcart();
        }
      }
    } catch (err) {
      err(err);
    }
  },
  topay() {
    to("/pages/newpay/newpay");
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true,
    });
    this.getlist();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
